package top.jle.spring.basic.util;

import com.alibaba.fastjson2.JSONObject;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class GlobalThreadLocalUtil {

    private static final ThreadLocal<Long> userIdLocal = ThreadLocal.withInitial(()->null);

    private static final ThreadLocal<String> jwtLocal = ThreadLocal.withInitial(()->null);

    private static final ThreadLocal<JSONObject> jwtBodyLocal = ThreadLocal.withInitial(()->null);

    private static final ThreadLocal<String> ipLocal = ThreadLocal.withInitial(()->null);

    public static void setUserId(Long userId) {

        userIdLocal.set(userId);
    }

    public static Long getUserId() {

        return userIdLocal.get();
    }

    public static void setJwt(String jwt) {

        jwtLocal.set(jwt);

    }
    public static String getJwt(){

        return jwtLocal.get();

    }

    public static JSONObject getJwtBody(){
        return jwtBodyLocal.get();
    }
    public static void setJwtBody(JSONObject jwtBody){
        jwtBodyLocal.set(jwtBody);
    }
    public static void setIp(String ip){
        ipLocal.set(ip);
    }

    public static String getIp() {
        return ipLocal.get();
    }

    public static void clearAll(){
        jwtLocal.remove();
        userIdLocal.remove();
        ipLocal.remove();
        jwtBodyLocal.remove();
    }

}
